package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DatabaseConnection;
import model.User;

public class UserdaoImp implements Userdao{

	private Connection con;
	//private PreparedStatement stat = null;
	public UserdaoImp(){
		
			//DatabaseConnection dac = new DatabaseConnection();
		//con=dac.getConnection();
	}
	public boolean doCreate(User user) throws Exception {
		String sql;
		String password = user.getpassword();
		String userid = user.getuserid();
		String username = user.getusername();
		int judge= 0;
		try {
			sql = "insert into users(userid,"
					+ "username,password) " + "values('" + userid
					+ "','" + username + "','" +  password + "')";
			PreparedStatement rs = con.prepareStatement(sql);
			judge = rs.executeUpdate();
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(judge>0)
			return true;
		else
			return false;
		}
	


	public boolean dodelete(String userid) throws Exception {
		
		String sql;
		int judge = 0;
		try {
			sql = "delete from users where cardid = '" + userid+ "'";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.executeQuery(sql);
			judge=stmt.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(judge>0)
			return true;
		else
			return false;
	}
	

	
	public boolean doupdate(User user) throws Exception {
	
		String sql;
		String password = user.getpassword();
		String userID = user.getuserid();
		int judge = 0;
		try {
			sql = "update users set password = '" + password + "' where cardid = '"
					+ userID + "'";
			PreparedStatement rs = con.prepareStatement(sql);
			judge = rs.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(judge>0)
			return true;
		else
			return false;
	}


	public User findUserByID(String userid) throws Exception {
		String sql;
		String username;
        String password;
        User user = new User();
		try {
			sql = "Select name,password from users where cardid = '" + userid + "' ";
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
					username = rs.getString(1);
					password = rs.getString(2);
					user.setusername(username);
					user.setpassword(password);
				}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return user;
	}

	
	public List<User> findAll() throws Exception {
		
		String sql;
		String userid;
		String username;
        String password;
        User user = new User();
		List<User> list=new ArrayList<User>();
		try {
			sql = "Select * from users";
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
					userid = rs.getString(1);
					username = rs.getString(2);
					password = rs.getString(3);
					user.setuserid(userid);
					user.setusername(username);
					user.setpassword(password);
					list.add(user);
				}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return list;
	}

}
